package leetcode;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:括号匹配
 * User: 东莞呵呵
 * Date:2022-07-14
 * Time:11:07
 */
public class TestDemo1 {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            if(ch=='('||ch=='['||ch=='{'){
                stack.push(ch);
            }else{
                if(!stack.empty()){
                    char top=stack.pop();
                    if(top=='('&&ch==')'||top=='{'&&ch=='}'||top=='['&&ch==']'){
                        continue;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }
}
